<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/15
 * Time: 17:38
 */

namespace App\Rep\Rbac;


use App\Models\Rbac\Node;
use App\Models\Rbac\Role;
use Illuminate\Support\Facades\DB;

class NodeRep
{
    //获取权限列表
    public function getNodeListRep($params){
        $data = Node::Search($params)
            ->select('id', 'name as title','route','description','created_at','updated_at','pid','icon','is_menu','path','sort','is_del','component','redirect')
            ->orderBy('sort','asc')->get()->toArray();
        $list = $this->subTree($data);
        return $list;
    }
    /**
     * 数据多层级
     * @param array $data
     * @param int $pid
     * @return array
     */
    public function subTree($data,  $pid = 0) {
        // 返回的结果
        $arr = [];
        foreach ($data as $val) {
            // 给定的PID是当前记录的上级ID

            if ($pid == $val['pid']) {
                $val['value'] = $val['id'];
                // 递归
                $val['children'] = $this->subTree($data,$val['id']);
                if(empty($val['children'])){
                    unset($val['children']);
                }
                $arr[] = $val;
            }
        }
        return $arr;
    }

    //添加权限
    public function createNodeRep($params){
        DB::beginTransaction();
        try{
            $data['name'] = $params['name'];
            $data['route'] = $params['route']??0;
            $data['description'] = $params['description'];
            $data['pid'] = $params['pid'];
            $data['icon'] = $params['icon']??'';
            $data['sort'] = $params['sort']??99;
            $data['is_menu'] = $params['is_menu'];
            if(isset($params['path'])){
                $data['path'] = $params['path']??'';
            }
            $data['component'] = $params['component']??'';
            $data['redirect'] = $params['redirect']??'';
            $where['id'] = $params['id']??'';
            Node::updateOrCreate($where,$data);
            DB::commit();
            return ['code'=>0,'data'=>[],'msg'=>''];
        }catch(\Exception $e){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
        }

    }

    //删除权限
    public function delNodeRep($params){
        $model = Node::find($params['id']);
        $model->is_del = $params['is_del'];
        $model->save();
        return $model;
    }

    /**
     * 获取菜单栏
     * @param $role_id
     */
    public function getMenuRep($role_id){
        if($role_id == 1){
            //超级管理员，获取所有权限
            $data = Node::where('is_del',2)->where('is_menu',1)->orderBy('sort','asc')->get()->toArray();
        }else{
            $data = Role::where('id',$role_id)->with('node')->where('is_del',2)->first()->toArray();
            //p($data);
            $data = $data['node'];
        }
        //p($role_id);
        $list = subTree($data);

        return $list;
    }

    public function getMenuAndButtonRep($role_id){
        if($role_id == 1){
            //超级管理员，获取所有权限
            $data = Node::where('is_del',2)->orderBy('sort','asc')->get()->toArray();
        }else{
            $data = Role::where('id',$role_id)->with('nodeAndButton')->where('is_del',2)->first()->toArray();
            //p($data);
            $data = $data['node_and_button'];
        }
        //p($data);
        $list = subTree($data);

        return $list;
    }
}